home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / Watcher / to_string.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-26  |  894 b   |  42 lines

  1. /*
  2.    to_string: convert a struct anything to a string, no matter what type
  3.    it is currently.
  4.  
  5.    I'm not completely happy with this; should make better use of
  6.    the info we are provided about the type of any instead of calling
  7.    this routine.
  8.  
  9.    Note that we assume that a real or integer will fit in a string
  10.    of length MAX_STR.
  11.  
  12.    Note also that we return a pointer to a static area.
  13.  
  14.    Kenneth Ingham
  15. */
  16.  
  17. #include "defs.h"
  18. #include "y.tab.h"
  19.  
  20. char *
  21. to_string(any)
  22. struct everything *any;
  23. {
  24.     static char rvalue[MAX_STR];
  25.  
  26.     switch (any->type) {
  27.         case STRING:
  28.             return any->data.string;
  29.         case FLOAT:
  30.             sprintf(rvalue, "%f", any->data.real);
  31.             return rvalue;
  32.         case INTEGER:
  33.             sprintf(rvalue, "%d", any->data.integer);
  34.             return rvalue;
  35.         default: 
  36.             fprintf(stderr, "Unable to convert type %d",any->type);
  37.             fprintf(stderr, " to string.\n");
  38.             exit(1);
  39.     }
  40.     /*NOTREACHED*/
  41. }
  42.